<div class="container-xl">
  <!-- Page title -->
  <div class="page-header d-print-none">
    <div class="row align-items-center">
      <div class="col d-none d-md-block">
        <h2 class="page-title text-nowrap">
          资源搜索
        </h2>
        {% if Results|length > 0 %}
        <div class="text-muted mt-1 d-none d-md-block">共搜索到 {{ Count }} 条记录</div>
        {% endif %}
      </div>
      <div class="col ms-auto d-print-none">
        <div class="d-flex btn-list">
          <div class="input-group">
            <input id="search_word" class="form-control" type="text" value="" placeholder="电影/电视剧名称">
            <button id="search_btn" class="btn btn-primary d-none d-sm-inline-block"
                    onclick="search_media_list()">搜索
            </button>
            <button id="search_icon" onclick="search_media_list()"
                    class="btn btn-primary d-sm-none btn-icon">
              <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-search"
                   width="24" height="24"
                   viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none"
                   stroke-linecap="round"
                   stroke-linejoin="round">
                <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
                <circle cx="10" cy="10" r="7"></circle>
                <line x1="21" y1="21" x2="15" y2="15"></line>
              </svg>
            </button>
            <button id="quick_search_btn" data-bs-toggle="dropdown" type="button"
                    class="btn btn-primary dropdown-toggle dropdown-toggle-split"
                    style="border-top-right-radius:4px;border-bottom-right-radius:4px;"></button>
            <div class="dropdown-menu dropdown-menu-end">
              <a class="dropdown-item" href="javascript:search_media(true)">
                快速搜索
              </a>
              <a class="dropdown-item" href="javascript:show_search_advanced_modal()">
                高级搜索
              </a>
            </div>
            {% if SearchWord %}
            <a href='javascript:navmenu("medialist?s={{ SearchWord }}&f=1&t=search")' class="btn btn-icon ms-1" title="返回"
              style="border-top-left-radius:4px;border-bottom-left-radius:4px;">
              <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-arrow-back-up" width="40"
                   height="40" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none"
                   stroke-linecap="round"
                   stroke-linejoin="round">
                <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
                <path d="M9 13l-4 -4l4 -4m-4 4h11a4 4 0 0 1 0 8h-1"></path>
              </svg>
            </a>
            {% endif %}
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
<!-- 业务页面代码 -->
{% if Count > 0 and Results|length > 0 %}
  <div class="page-body">
    <div class="container-xl">
      <div class="row">
        <div class="col">
          <div class="row row-cards" id="media_cards">
            {% for Title, Item in Results.items() %}
              <div class="card">
                <div class="ribbon g-cyan d-md-none">
                  <a class="text-white" href="javascript:show_search_filter_modal('{{ Item.key }}')">
                    筛选
                  </a>
                </div>
                <!-- 是否存在 -->
                {% if Item.exist %}
                <div class="badge badge-pill bg-green"
                     style="position:absolute;top:20px;right:50px;padding:0;">
                  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"
                       fill="currentColor" aria-hidden="true"
                       class="h-3 w-3 sm:h-4 sm:w-4">
                    <path fill-rule="evenodd"
                          d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z"
                          clip-rule="evenodd"></path>
                  </svg>
                </div>
                {% endif %}
                <div class="card-body ps-1 pe-1">
                  <div class="row">
                    <div class="col-2 d-none d-md-block" id="search_results_filter_{{ Item.key }}">
                      <!-- 图片 -->
                      {% if Item.poster %}
                      <img class="rounded object-cover shadow" src="{{ Item.poster }}" alt="">
                      {% endif %}
                      <!-- 过滤条件 -->
                      {% if Item.filter.season %}
                      <div class="subheader mb-2 mt-2">季</div>
                      <div class="form-selectgroup form-selectgroup-pills">
                        {% for filter_season in Item.filter.season %}
                        <label class="form-selectgroup-item">
                          <input type="checkbox" name="filter_season" value="{{ filter_season }}" class="form-selectgroup-input"
                            onclick="filter_medias(this, '{{ Item.key }}')">
                          <span class="form-selectgroup-label">{{ filter_season }}</span>
                        </label>
                        {% endfor %}
                      </div>
                      {% endif %}
                      <div class="subheader mb-2 mt-3">站点</div>
                      <div class="form-selectgroup form-selectgroup-pills">
                        {% for filter_site in Item.filter.site %}
                        <label class="form-selectgroup-item">
                          <input type="checkbox" name="filter_site" value="{{ filter_site }}" class="form-selectgroup-input"
                            onclick="filter_medias(this, '{{ Item.key }}')">
                          <span class="form-selectgroup-label">{{ filter_site }}</span>
                        </label>
                        {% endfor %}
                      </div>
                      <div class="subheader mb-2 mt-2">促销</div>
                      <div class="form-selectgroup form-selectgroup-pills">
                        {% for filter_free in Item.filter.free %}
                        <label class="form-selectgroup-item">
                          <input type="checkbox" name="filter_free" value="{{ filter_free.value }}" class="form-selectgroup-input"
                            onclick="filter_medias(this, '{{ Item.key }}')">
                          <span class="form-selectgroup-label">{{ filter_free.name }}</span>
                        </label>
                        {% endfor %}
                      </div>
                      {% if Item.filter.video %}
                      <div class="subheader mb-2 mt-2">视频编码</div>
                      <div class="form-selectgroup form-selectgroup-pills">
                        {% for filter_video in Item.filter.video %}
                        <label class="form-selectgroup-item">
                          <input type="checkbox" name="filter_video" value="{{ filter_video }}" class="form-selectgroup-input"
                            onclick="filter_medias(this, '{{ Item.key }}')">
                          <span class="form-selectgroup-label">{{ filter_video }}</span>
                        </label>
                        {% endfor %}
                      </div>
                      {% endif %}
                      <div class="mt-3">
                        <button class="btn w-100" onclick="reset_filter(this, '{{ Item.key }}')">
                          重置
                        </button>
                      </div>
                    </div>
                    <div class="col">
                      <!-- 标题 -->
                      <div class="row">
                        <div class="col">
                          <h3 class="mb-0">
                            {% if Item.tmdbid and Item.tmdbid != '0' %}
                            <strong>{{ Title }}</strong>
                            {% else %}
                            <strong class="ms-3">{{ Title }}</strong>
                            {% endif %}
                          </h3>
                        </div>
                      </div>
                      <!-- 媒体属性 -->
                      <div class="row mt-2">
                        <div class="col-md">
                          {% if Item.tmdbid and Item.tmdbid!= '0' %}
                          <div class="list-inline list-inline-dots mb-0 text-muted">
                            <div class="list-inline-item">
                              <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-video" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
                                 <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
                                 <path d="M15 10l4.553 -2.276a1 1 0 0 1 1.447 .894v6.764a1 1 0 0 1 -1.447 .894l-4.553 -2.276v-4z"></path>
                                 <rect x="3" y="6" width="12" height="12" rx="2"></rect>
                              </svg>
                              {{ Item.type }}
                            </div>
                            <div class="list-inline-item">
                              <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-star" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
                                 <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
                                 <path d="M12 17.75l-6.172 3.245l1.179 -6.873l-5 -4.867l6.9 -1l3.086 -6.253l3.086 6.253l6.9 1l-5 4.867l1.179 6.873z"></path>
                              </svg>
                              {{ Item.vote or '暂无评分' }}
                            </div>
                            <div class="list-inline-item">
                              <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-info-circle" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
                                 <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
                                 <circle cx="12" cy="12" r="9"></circle>
                                 <line x1="12" y1="8" x2="12.01" y2="8"></line>
                                 <polyline points="11 12 12 12 12 16 13 16"></polyline>
                              </svg>
                              <a href="{% if Item.type == '电影' %}https://www.themoviedb.org/movie/{{ Item.tmdbid }}{% else %}https://www.themoviedb.org/tv/{{ Item.tmdbid }}{% endif %}" target="_blank">{{ Item.tmdbid }}</a>
                            </div>
                          </div>
                          {% endif %}
                        </div>
                      </div>
                      <!-- 简介 -->
                      {% if Item.overview %}
                      <div class="text-muted mt-2 d-none d-md-block">
                        {{ Item.overview}}
                      </div>
                      {% endif %}
                      <!-- 资源分组 -->
                      {% for SE_key, SE_dict in Item.torrent_dict %}
                      {% if SE_key != 'MOV' %}
                        <a href="javascript:$('#search_se_accordion_{{ SE_key.replace(' ', '') }}_{{ Item.key }}').slideToggle()"
                          class="search_results_season_{{ Item.key }}"
                          data-season="{{ SE_key }}">
                          <div class="row mt-2">
                            <h3 class="mb-0">
                            <strong>{{ SE_key }}</strong>
                            </h3>
                          </div>
                        </a>
                      {% endif %}
                      <div {% if SE_key != 'MOV' %} id="search_se_accordion_{{ SE_key.replace(' ', '') }}_{{ Item.key }}" {% endif %}>
                        <div class="accordion mt-2" id="search_results_accordion_{{ Item.key }}">
                          {% for group_key, group in SE_dict.items() %}
                          <div class="accordion-item" id="search_results_accordion_item_{{ group_key }}_{{ Item.key }}_{{ SE_key.replace(' ', '') }}">
                            <h2 class="accordion-header">
                              <button
                                  class="accordion-button p-2 {% if loop.index0 != 0 %}collapsed{% endif %}"
                                  type="button"
                                  data-bs-toggle="collapse"
                                  data-bs-target="#search_results_group_{{ group_key }}_{{ Item.key }}_{{ SE_key.replace(' ', '') }}"
                                  aria-expanded="true">
                                  <span class="text-red">{{ group.group_info.restype or "未知媒介" }}</span>
                                    &nbsp;/&nbsp;
                                  <span class="text-orange">{{ group.group_info.respix or "未知分辨率" }}</span>
                                    &nbsp;/&nbsp;
                                  <span class="text">共 <span id="search_results_group_total_{{ group_key }}_{{ Item.key }}_{{ SE_key.replace(' ', '') }}"
                                                           class="search_results_group_total"
                                                           data-total="{{ group.group_total }}">{{ group.group_total }}</span> 个种子 </span>
                              </button>
                            </h2>
                            <div id="search_results_group_{{ group_key }}_{{ Item.key }}_{{ SE_key.replace(' ', '') }}"
                                 class="accordion-collapse collapse {% if loop.index0 == 0 %}show{% endif %}"
                                 data-bs-parent="#search_results_accordion_{{ Item.key }}">
                              <div class="accordion-body pt-0">
                                <div class="list-group">
                                  <div class="list-group card-list-group mt-2">
                                    <!-- 种子列表 -->
                                  {% for unique_key, unique in group.group_torrents.items() %}
                                    {% for torrent in unique.torrent_list %}
                                      <div class="list-group-item p-2 ps-1 search_results_torrent"
                                        data-site="{{ torrent.site }}"
                                        data-free="{{ torrent.uploadvalue|string + ' ' + torrent.downloadvalue|string }}"
                                        data-video="{{ torrent.video_encode }}"
                                        data-group="{{ group_key }}"
                                        data-season="{{ SE_key.replace(' ', '') }}">
                                        <div class="row g-2 align-items-center">
                                          <div class="col-auto">
                                            <span class="avatar avatar-sm rounded siteicon-{{ torrent.site|hash }}" style="overflow: hidden;">
                                            {% if SiteDict[torrent.site] %}
                                              {% if SiteDict[torrent.site].public %}
                                              BT
                                              {% elif not SiteDict[torrent.site].builtin %}
                                              PT
                                              {% endif %}
                                            {% endif %}
                                            </span>
                                          </div>
                                          <div class="col">
                                            <a href='javascript:show_download_modal("{{ torrent.id }}","【{{ torrent.site }}】{{ torrent.torrent_name }}")'>{{ torrent.torrent_name }}</a>
                                            <div class="text-muted">
                                              {{ torrent.description or '' }}
                                            </div>
                                            <div>
                                              <span class="badge bg-black text-dark-fg mb-1">{{ torrent.site }}</span>
                                              {% if torrent.video_encode %}
                                              <span class="badge text-white bg-orange mb-1">{{ torrent.video_encode }}</span>
                                              {% endif %}
                                              {% if torrent.reseffect %}
                                                <span class="badge text-white bg-purple mb-1">{{ torrent.reseffect }}</span>
                                              {% endif %}
                                              {% if torrent.size %}
                                                <span class="badge text-white bg-yellow mb-1">{{ torrent.size }}</span>
                                              {% endif %}
                                              {% if torrent.releasegroup %}
                                                <span class="badge text-white bg-cyan mb-1">{{ torrent.releasegroup }}</span>
                                              {% endif %}
                                              {% if torrent.uploadvalue != 1.0 %}
                                              <span class="badge text-white bg-azure mb-1">{{ (torrent.uploadvalue * 100) | int }}%UL</span>
                                              {% endif %}
                                              {% if torrent.downloadvalue != 1.0 %}
                                                {% if torrent.downloadvalue == 0.0 %}
                                                  <span class="badge text-white bg-lime mb-1">FREE</span>
                                                {% else %}
                                                  <span class="badge text-white bg-indigo mb-1">{{ (torrent.downloadvalue * 100) | int }}%DL</span>
                                                {% endif %}
                                              {% endif %}
                                              <span class="badge text-white mb-1">{{ torrent.seeders }}{{ UPCHAR }}</span>
                                            </div>
                                          </div>
                                          <div class="col-auto ms-2 d-none d-md-inline-block">
                                            <a href='javascript:show_download_modal("{{ torrent.id }}","【{{ torrent.site }}】{{ torrent.torrent_name }}")' class="link-secondary" title="下载">
                                              <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-download" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
                                                 <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
                                                 <path d="M4 17v2a2 2 0 0 0 2 2h12a2 2 0 0 0 2 -2v-2"></path>
                                                 <polyline points="7 11 12 16 17 11"></polyline>
                                                 <line x1="12" y1="4" x2="12" y2="16"></line>
                                              </svg>
                                            </a>
                                          </div>
                                          <div class="col-auto lh-1 ms-2 d-none d-md-inline-block">
                                            <div class="dropdown">
                                              <a href="#" class="link-secondary" data-bs-toggle="dropdown" aria-expanded="false">
                                                <svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"></path><circle cx="5" cy="12" r="1"></circle><circle cx="12" cy="12" r="1"></circle><circle cx="19" cy="12" r="1"></circle></svg>
                                              </a>
                                              <div class="dropdown-menu dropdown-menu-end" style="">
                                                <a class="dropdown-item" href="{{ torrent.enclosure }}" target="_blank">
                                                  下载种子文件
                                                </a>
                                                <a class="dropdown-item" href="{{ torrent.pageurl }}" target="_blank">
                                                  查看种子详情
                                                </a>
                                              </div>
                                            </div>
                                          </div>
                                        </div>
                                      </div>
                                    {% endfor %}
                                  {% endfor %}
                                  </div>
                                </div>
                              </div>
                            </div>
                          </div>
                          {% endfor %}
                        </div>
                      </div>
                      {% endfor %}
                    </div>
                  </div>
                </div>
              </div>
            {% endfor %}
          </div>
        </div>
      </div>
    </div>
  </div>
{% else %}
  <div class="page-body">
    <div class="container-xl d-flex flex-column justify-content-center">
      <div class="empty">
        <div class="empty-img"><img src="./static/img/posting_photo.svg" height="128" alt="">
        </div>
        <p class="empty-title" id="search_tip_title">没有记录</p>
        <p class="empty-subtitle text-muted" id="search_tip_text">
          输入想看的电影、电视剧名称，点击搜索试试看吧。
        </p>
      </div>
    </div>
  </div>
{% endif %}
<div class="modal modal-blur fade" id="modal-search-advanced" tabindex="-1" role="dialog" aria-hidden="true"
     data-bs-backdrop="static" data-bs-keyboard="false">
  <div class="modal-dialog modal-lg modal-dialog-centered modal-dialog-scrollable" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">高级搜索</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">
        <div class="row">
          <div class="col-12 col-lg-4">
            <div class="mb-3">
              <label class="form-label">类型</label>
              <select class="form-select" id="search_type">
                <option value="" selected>全部</option>
                <option value="电影">电影</option>
                <option value="电视剧">电视剧</option>
              </select>
            </div>
          </div>
          <div class="col-12 col-lg-4">
            <div class="mb-3">
              <label class="form-label required">名称</label>
              <input type="text" value="" id="search_name" class="form-control"
                     placeholder="电影/电视剧名称">
            </div>
          </div>
          <div class="col-12 col-lg-2">
            <div class="mb-3">
              <label class="form-label">年份</label>
              <input type="text" value="" id="search_year" class="form-control" placeholder="20xx">
            </div>
          </div>
          <div class="col-12 col-lg-2">
            <div class="mb-3">
              <label class="form-label">季</label>
              <select class="form-select" id="search_season">
                <option value="" selected>全部</option>
              </select>
            </div>
          </div>
        </div>
        <div class="row">
          <div class="col-12 col-lg-4">
            <div class="mb-3">
              <label class="form-label">站点</label>
              <select class="form-select" id="search_site">
                <option value="" selected>全部</option>
                {% for site in SiteDict.keys() %}
                  <option value="{{ site }}">{{ site }}</option>
                {% endfor %}
              </select>
            </div>
          </div>
          <div class="col-12 col-lg-4">
            <div class="mb-3">
              <label class="form-label">质量</label>
              <select class="form-select" id="search_restype">
                <option value="" selected>全部</option>
                {% for Restype in RestypeDict %}
                  <option value="{{ Restype }}">{{ Restype }}</option>
                {% endfor %}
              </select>
            </div>
          </div>
          <div class="col-12 col-lg-4">
            <div class="mb-3">
              <label class="form-label">分辨率</label>
              <select class="form-select" id="search_pix">
                <option value="" selected>全部</option>
                {% for Pix in PixDict %}
                  <option value="{{ Pix }}">{{ Pix }}</option>
                {% endfor %}
              </select>
            </div>
          </div>
          <div class="col-12 col-lg-4">
            <div class="mb-3">
              <label class="form-label">促销</label>
              <select class="form-select" id="sp_state">
                <option value="* *" selected>全部</option>
                <option value="1.0 1.0">普通</option>
                <option value="1.0 0.0">免费</option>
                <option value="2.0 1.0">2X</option>
                <option value="2.0 0.0">2X免费</option>
                <option value="1.0 0.5">50%</option>
                <option value="2.0 0.5">2X 50%</option>
                <option value="1.0 0.7">70%</option>
                <option value="1.0 0.3">30%</option>
              </select>
            </div>
          </div>
          <div class="col-12 col-lg-8">
            <div class="mb-3">
              <label class="form-label">自定义</label>
              <input type="text" value="" id="search_key" class="form-control"
                     placeholder="自定义过滤规则">
            </div>
          </div>
        </div>
      </div>
      <div class="modal-footer">
        <button onclick="search_media_advanced()" id="search_advanced_btn"
                class="btn btn-primary">开始搜索
        </button>
      </div>
    </div>
  </div>
</div>
<!-- 搜索结果过滤弹窗 -->
{% for Title, Item in Results.items() %}
<div class="modal modal-blur fade" id="search-filter-modal-{{ Item.key }}" tabindex="-1" role="dialog" aria-hidden="true"
     data-bs-backdrop="static" data-bs-keyboard="false">
  <div class="modal-dialog modal-lg modal-dialog-centered" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">过滤: {{ Title }}</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">
        <div class="row">
          <div class="col" id="search_results_filter_{{ Item.key }}_modal">
            <!-- 过滤条件 -->
            {% if Item.filter.season %}
            <div class="subheader mb-2 mt-2">季</div>
            <div class="form-selectgroup form-selectgroup-pills">
              {% for filter_season in Item.filter.season %}
              <label class="form-selectgroup-item">
                <input type="checkbox" name="filter_season" value="{{ filter_season }}" class="form-selectgroup-input"
                  onclick="filter_medias(this, '{{ Item.key }}', true)">
                <span class="form-selectgroup-label">{{ filter_season }}</span>
              </label>
              {% endfor %}
            </div>
            {% endif %}
            <div class="subheader mb-2 mt-3">站点</div>
            <div class="form-selectgroup form-selectgroup-pills">
              {% for filter_site in Item.filter.site %}
              <label class="form-selectgroup-item">
                <input type="checkbox" name="filter_site" value="{{ filter_site }}" class="form-selectgroup-input"
                  onclick="filter_medias(this, '{{ Item.key }}', true)">
                <span class="form-selectgroup-label">{{ filter_site }}</span>
              </label>
              {% endfor %}
            </div>
            <div class="subheader mb-2 mt-2">促销</div>
            <div class="form-selectgroup form-selectgroup-pills">
              {% for filter_free in Item.filter.free %}
              <label class="form-selectgroup-item">
                <input type="checkbox" name="filter_free" value="{{ filter_free.value }}" class="form-selectgroup-input"
                  onclick="filter_medias(this, '{{ Item.key }}', true)">
                <span class="form-selectgroup-label">{{ filter_free.name }}</span>
              </label>
              {% endfor %}
            </div>
            {% if Item.filter.video %}
            <div class="subheader mb-2 mt-2">视频编码</div>
            <div class="form-selectgroup form-selectgroup-pills">
              {% for filter_video in Item.filter.video %}
              <label class="form-selectgroup-item">
                <input type="checkbox" name="filter_video" value="{{ filter_video }}" class="form-selectgroup-input"
                  onclick="filter_medias(this, '{{ Item.key }}', true)">
                <span class="form-selectgroup-label">{{ filter_video }}</span>
              </label>
              {% endfor %}
            </div>
            {% endif %}
          </div>
        </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn me-auto" onclick="reset_filter(this, '{{ Item.key }}', true)">重置</button>
        <button type="button" class="btn btn-primary" data-bs-dismiss="modal">确定</button>
      </div>
    </div>
  </div>
</div>
{% endfor %}

<script type="text/javascript">
  // 关键字
  var search_word = "{{ SearchWord }}";
  var need_search = "{{ NeedSearch }}";
  var result_count = {{ Count }};
  if (search_word !== "") {
    $("#search_word").val(decodeURI(search_word));
    if (need_search) {
      search_media(true);
    } else if (!result_count) {
      $("#search_tip_title").text(`${search_word} 未找到任何资源`)
      $("#search_tip_text").text("")
    }
  }

  // 搜索按钮
  function search_media(unident) {
    if (!unident) {
      unident = "";
    } else {
      unident = "1";
    }
    const keyword = $("#search_word").val();
    if (keyword === "") {
      return;
    }
    $("#search_btn").text("搜索中...").attr("disabled", "true");
    $("#quick_search_btn").attr("disabled", "true");
    $("#search_icon").attr("disabled", "true");
    $("#search_tip_title").text(`正在搜索 ${keyword}，请稍后...`);
    $("#search_tip_text").text(`正在搜索 ${keyword} ...`);
    const param = {"search_word": keyword, "unident": unident};
    show_refresh_process(`正在搜索 ${keyword} ...`);
    ajax_post("search", param, function (ret) {
      hide_refresh_process();
      if (ret.code === 0) {
        navmenu(`search?s=${keyword}`);
      } else {
        $("#search_btn").text("搜索").removeAttr("disabled");
        $("#quick_search_btn").removeAttr("disabled");
        $("#search_icon").removeAttr("disabled");
        $("#search_tip_title").text("搜索失败");
        $("#search_tip_text").text("请确认名称是否正确，网络连接是否正常");
        show_fail_modal(ret.msg);
      }
    });
  }

  //搜索联想
  function search_media_list() {
    const keyword = $("#search_word").val();
    if (keyword === "") {
      return;
    }
    navmenu(`medialist?s=${keyword}&f=1&t=search`);
  }

  // 搜索输入框
  $('#search_word').bind('keypress', function (event) {
    if (event.keyCode === 13) {
      search_media_list();
    }
  });

  // 计算各分组的种子数量
  function sub_group_total(group, key, se) {
    let total_obj = $(`#search_results_group_total_${group}_${key}_${se}`)
    let current_num = parseInt(total_obj.text());
    if (current_num < 1) {
      current_num = 1;
    }
    total_obj.text(current_num-1);
    if (current_num===1) {
      $(`#search_results_accordion_item_${group}_${key}_${se}`).hide();
    }
  }

  // 执行过滤
  function filter_do(key, modal) {
    let filter_obj;
    if (modal) {
      filter_obj = `#search_results_filter_${key}_modal`;
    } else {
      filter_obj = `#search_results_filter_${key}`;
    }
    let torrent_obj = `#search_results_accordion_${key}`;
    let season_obj = `.search_results_season_${key}`;
    // 全部显示
    $(`${torrent_obj} .accordion-item`).each(function () {
      $(this).show();
    });
    $(`${torrent_obj} .search_results_torrent`).each(function () {
      $(this).show();
    });
    $(season_obj).each(function () {
      $(this).show();
    });
    // 恢复总条数
    $(`${torrent_obj} .search_results_group_total`).each(function () {
      $(this).text($(this).attr("data-total"));
    });
    // 过滤站点
    let filter_site;
    $(`${filter_obj} input[name=filter_site]`).each(function () {
      if($(this).prop('checked')){
        filter_site = $(this).val();
      }
    });
    // 过滤促销
    let filter_free;
    $(`${filter_obj} input[name=filter_free]`).each(function () {
      if($(this).prop('checked')){
        filter_free = $(this).val();
      }
    });
    // 过滤编码
    let filter_video;
    $(`${filter_obj} input[name=filter_video]`).each(function () {
      if($(this).prop('checked')){
        filter_video = $(this).val();
      }
    });
    //过滤剧集
    let filter_season;
    $(`${filter_obj} input[name=filter_season]`).each(function () {
      if($(this).prop('checked')){
        filter_season = $(this).val();
      }
    });
    // 有过滤则展开，无过滤则收起
    if (filter_site || filter_free || filter_video) {
      $(`${torrent_obj} .accordion-button`).each(function () {
        $(this).removeClass("collapsed");
      });
      $(`${torrent_obj} .accordion-collapse`).each(function () {
        $(this).addClass("show");
      });
    } else {
      $(`${torrent_obj} .accordion-button`).each(function () {
        $(this).addClass("collapsed");
      });
      $(`${torrent_obj} .accordion-collapse`).each(function () {
        $(this).removeClass("show");
      });
    }
    // 执行过滤
    if (filter_site) {
      $(`${torrent_obj} .search_results_torrent`).each(function () {
        if ($(this).attr("data-site") !== filter_site) {
          if($(this).is(":visible")){
            sub_group_total($(this).attr("data-group"), key, $(this).attr("data-season"));
            $(this).hide();
          }
        }
      });
    }
    if (filter_free) {
      $(`${torrent_obj} .search_results_torrent`).each(function () {
        if ($(this).attr("data-free") !== filter_free) {
          if($(this).is(":visible")){
            $(this).hide();
            sub_group_total($(this).attr("data-group"), key, $(this).attr("data-season"));
          }
        }
      });
    }
    if (filter_video) {
      $(`${torrent_obj} .search_results_torrent`).each(function () {
        if ($(this).attr("data-video") !== filter_video) {
          if($(this).is(":visible")) {
            $(this).hide();
            sub_group_total($(this).attr("data-group"), key, $(this).attr("data-season"));
          }
        }
      });
    }
    if (filter_season) {
      $(season_obj).each(function () {
        let season = $(this).attr("data-season").split(" ")[0];
        let se_key = $(this).attr("data-season").replace(" ", "");
        if ( season !== filter_season) {
          $(this).hide();
          $(`#search_se_accordion_${se_key}_${key}`).hide();
        }else{
          $(this).show();
          $(`#search_se_accordion_${se_key}_${key}`).show();
        }
      });
    }
  }

  // 过滤媒体展示
  function filter_medias(obj, key, modal) {
    // 处理UI
    $(obj).parent().parent().find("input[type=checkbox]").each(function(){
      $(this).prop("checked", false);
    });
    $(obj).prop("checked", true);
    // 开始过滤
    filter_do(key, modal);
  }

  // 重置过滤条件
  function reset_filter(obj, key, modal) {
    // 处理UI
    $(obj).parent().parent().find("input[type=checkbox]").each(function(){
      $(this).prop("checked", false);
    });
    // 开始过滤
    filter_do(key, modal);
  }

  // 显示过滤器弹窗
  function show_search_filter_modal(key) {
    $(`#search-filter-modal-${key}`).modal("show");
  }

  // 显示高级搜索框
  function show_search_advanced_modal() {
    $("#modal-search-advanced").modal("show");
  }

  // 开始高级搜索
  function search_media_advanced() {
    let keyword;
    $("#search_icon").attr("disabled", "true");
    // 读取数据
    const search_name = $("#search_name").val();
    if (!search_name) {
      $("#search_name").addClass("is-invalid");
      return;
    } else {
      $("#search_name").removeClass("is-invalid");
    }
    const search_type = $("#search_type").val();
    const search_season = $("#search_season").val();
    const search_year = $("#search_year").val();
    const search_site = $("#search_site").val();
    const search_restype = $("#search_restype").val();
    const search_pix = $("#search_pix").val();
    const sp_state = $("#sp_state").val();
    const search_key = $("#search_key").val();
    // 拼装请求
    if (search_type) {
      keyword = search_type + " " + search_name;
    } else {
      keyword = search_name;
    }
    if (search_year) {
      keyword = keyword + " " + search_year;
    }
    if (search_season) {
      keyword = keyword + " " + search_season;
    }
    const filters = {
      "site": search_site,
      "restype": search_restype,
      "pix": search_pix,
      "sp_state": sp_state,
      "key": search_key
    };
    const param = {"search_word": keyword, "filters": filters, "unident": true};
    $("#modal-search-advanced").modal("hide");
    show_refresh_process(`正在搜索 ${keyword} ...`);
    ajax_post("search", param, function (ret) {
      hide_refresh_process();
      if (ret.code === 0) {
        navmenu(`search?s=${keyword}`);
      } else {
        show_fail_modal(ret.msg,function () {
          $("#modal-search-advanced").modal("show");
        });
      }
    });
  }

  //初始化下拉框
  $("#search_season").empty().append('<option value="">全部</option>');
  for (let i = 1; i <= 50; i++) {
    $("#search_season").append(`<option value="第${i}季">第${i}季</option>`);
  }

</script>
